package com.efast.dmom.mgmt.controller.mes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.dmom.mgmt.bean.mes.order.OrderCancelBean;
import com.efast.dmom.mgmt.bean.mes.order.OrderCloseBean;
import com.efast.dmom.mgmt.bean.mes.order.RecycledVinBean;
import com.efast.dmom.mgmt.bean.mes.order.VinManualFixBean;
import com.efast.dmom.mgmt.bean.mes.order.WorkOrderItem;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.entity.mes.TwBasWorkOrder;
import com.efast.dmom.mgmt.service.mes.TwBasWorkOrderService;
import com.efast.dmom.mgmt.service.mes.order.VinRecycleService;
import com.efast.dmom.mgmt.utils.MesConstant;

@Controller
@RequestMapping(value = "/twBasWorkOrder")
public class TwBasWorkOrderController {

	@Autowired
	private TwBasWorkOrderService twBasWorkOrderService;
	@Autowired
	private VinRecycleService vinRecycleService;

	// 工单创建[生成工单号，工单状态改为CREATED]
	@RequestMapping(value = "/save")
	public @ResponseBody ResultBean save(@RequestBody TwBasWorkOrder twBasWorkOrder) {
		ResultBean result = new ResultBean(true, "'");
		twBasWorkOrderService.insertTwBasWorkOrder(twBasWorkOrder);
		return result;
	}

	// 工单拆分
	@RequestMapping(value = "/split")
	public @ResponseBody ResultBean splitWorkOrder(@RequestBody Map<Object, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		twBasWorkOrderService.splitWorkOrder((Integer) map.get("orderId"),
				((Integer) map.get("splitQty")).doubleValue(), (Integer) map.get("splitBatch"));
		return result;
	}

	// 工单合并
	@RequestMapping(value = "/merge")
	public @ResponseBody ResultBean mergeWorkOrder(@RequestBody Map<Object, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		twBasWorkOrderService.mergeWorkOrder((Integer) map.get("orderId"), (String) map.get("orderNum"));
		return result;
	}

	// 强制关闭工单
	@RequestMapping(value = "/close")
	public @ResponseBody ResultBean closeWorkOrder(@RequestBody OrderCloseBean orderCloseBean) {
		ResultBean result = twBasWorkOrderService.closeWorkOrder(orderCloseBean.getCloseReason(), orderCloseBean.getPwd(),
				orderCloseBean.getWorkOrder());
		return result;
	}

	// 发布工单
	@RequestMapping(value = "/release")
	public @ResponseBody ResultBean releaseWorkOrder(@RequestBody List<TwBasWorkOrder> twBasWorkOrders) {
		ResultBean result = new ResultBean(true, "'");
		twBasWorkOrderService.releaseWorkOrder(twBasWorkOrders);
		return result;
	}

	// 获取所有工单号
	@RequestMapping(value = "/getAllOrderNum")
	public @ResponseBody ResultBean getAllOrderNum() {
		ResultBean result = new ResultBean(true, "'");
		String[] orderNum = twBasWorkOrderService.getAllOrderNum();
		result.setObject(orderNum);
		return result;
	}

	// 获取所有产线及产品定义的产线
	@RequestMapping(value = "/getAllProLines")
	public @ResponseBody ResultBean getAllProLines(@RequestBody List<TwBasWorkOrder> twBasWorkOrder) {
		ResultBean result = new ResultBean(true, "'");
		Map<Object, Object> obj = twBasWorkOrderService.getAllProLines(twBasWorkOrder);
		result.setObject(obj);
		result.setSuccess((boolean) obj.get("flag"));
		result.setMessage((String) obj.get("message"));
		return result;
	}

	// 产品序号生成
	@RequestMapping(value = "/generateSn")
	public @ResponseBody ResultBean generateSn(@RequestBody List<TwBasWorkOrder> twBasWorkOrders) {

		ResultBean result = new ResultBean(true, null);
		List<TwBasWorkOrder> twWorkOrderList = new ArrayList<TwBasWorkOrder>();
		if (twBasWorkOrders != null && twBasWorkOrders.size() > 0) {
			for (TwBasWorkOrder data : twBasWorkOrders) {
				if ("CANCELLED".equals(data.getStatus())) {
					result.setSuccess(false);
					result.setMessage("选择的订单存在已经取消的订单，请重新选择！");
					return result;
				} else {
					twWorkOrderList.add(data);
				}
			}
		}
		if (twWorkOrderList != null && twWorkOrderList.size() > 0) {
			twBasWorkOrderService.insertTwBasLot(twWorkOrderList);
			twBasWorkOrderService.releaseWorkOrder(twWorkOrderList);
		}
		return result;
	}

	@RequestMapping(value = "/holdLotInBatch")
	public @ResponseBody ResultBean holdLotInBatch(@RequestBody List<TwBasLot> beanList) {

		ResultBean result = new ResultBean(true, null);

		if (beanList != null) {
			twBasWorkOrderService.holdLotInBatch(beanList);
		}
		return result;
	}

	@RequestMapping(value = "/manualFixVin")
	public @ResponseBody ResultBean manualFixVin(@RequestBody VinManualFixBean fixBean) {

		ResultBean resultBean = new ResultBean(true, null);

		if (fixBean != null) {

			List<TwBasLot> lotBeanList = fixBean.getLotBeanList();
			List<RecycledVinBean> vinList = fixBean.getVinList();

			if (lotBeanList == null || vinList == null) {

				resultBean.setSuccess(false);
				resultBean.setMessage("所选回收池VIN数量和需替换数量不匹配");

				return resultBean;
			}

			List<TwBasLot> basLotList = new ArrayList<TwBasLot>();

			for (TwBasLot basLot : lotBeanList) {

				if (!MesConstant.LOT_STATE_HOLD.equals(basLot.getState())) {

					resultBean.setSuccess(false);
					resultBean.setMessage("请先冻结车辆VIN【" + basLot.getVin() + "】");

					return resultBean;
				}

				for (RecycledVinBean vinBean : vinList) {

					if (vinBean.getPartNoVersion() == null
							|| !vinBean.getPartNoVersion().equals(basLot.getPartNoVersion())) {

						resultBean.setSuccess(false);
						resultBean.setMessage("回收池VIN【" + vinBean.getVin() + "】成品物料号【" + vinBean.getPartNoVersion()
								+ "与被选车辆成品物料号不一致【" + basLot.getPartNoVersion());

						return resultBean;
					}

					String vin = vinBean.getVin();
					if (vin == null || "".equals(vin)) {

						resultBean.setSuccess(false);
						resultBean.setMessage("所选回收池VIN【" + vin + "】为空");

						return resultBean;
					}
					vinBean.setCurrentStatus(MesConstant.REUSED);
					basLot.setVin(vin);
					basLotList.add(basLot);
				}
			}
			twBasWorkOrderService.fixVinInBatch(basLotList);
			vinRecycleService.updateRecycleBeanStatus(vinList);
			twBasWorkOrderService.removeCachedRecycleBean(vinList);
		}

		return resultBean;
	}

	// 手动产品序号生成
	@RequestMapping(value = "/manualgenerateSn")
	public @ResponseBody ResultBean manualgenerateSn(@RequestBody Map<Object, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		twBasWorkOrderService.insertManualTwBasLot((Integer) map.get("orderId"), (Integer) map.get("lotQty"),
				(String) map.get("lotName"));
		return result;
	}

	// 产品包装通过扫入工单号获取工单信息及工单下所有lots
	@RequestMapping(value = "/getWorkOrderInfo")
	public @ResponseBody ResultBean getWorkOrderInfo(@RequestBody Map<Object, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		Map<String, Object> orderInfoMap = twBasWorkOrderService.getWorkOrderInfo((String) map.get("orderNumber"));
		if (orderInfoMap.get("workOrder") != null) {
			result.setObject(orderInfoMap);
		} else {
			result.setSuccess(false);
			result.setMessage("工单不存在，请重新扫入！");
		}
		return result;
	}

	// 按周获取工单总量（从当前周往前5周）
	@RequestMapping(value = "/getOrderCount")
	public @ResponseBody ResultBean getOrderCount() {
		ResultBean result = new ResultBean(true, "'");
		Map<String, Object> orderMap = twBasWorkOrderService.getOrderCount();
		result.setObject(orderMap);
		return result;
	}

	// 货车过点时没有VIN的订单补VIN
	@RequestMapping(value = "/replenish")
	public @ResponseBody void replenishVin() {
		twBasWorkOrderService.replenishVin();
	}

	// 工单冻结
	@RequestMapping(value = "/workOrderFreeze")
	public @ResponseBody ResultBean workOrderFreeze(@RequestBody List<TwBasWorkOrder> twBasWorkOrders) {
		ResultBean result = twBasWorkOrderService.workOrderFreeze(twBasWorkOrders);
		return result;
	}

	// 工单解冻
	@RequestMapping(value = "/workOrderThaw")
	public @ResponseBody ResultBean workOrderThaw(@RequestBody List<TwBasWorkOrder> twBasWorkOrders) {
		ResultBean result = twBasWorkOrderService.workOrderThaw(twBasWorkOrders);
		return result;
	}

	// 车辆报废
	@RequestMapping(value = "/vechicleScrap")
	public @ResponseBody ResultBean vechicleScrap(@RequestBody List<RecycledVinBean> beanList) {
		ResultBean result = new ResultBean(true, ",");
		twBasWorkOrderService.vechicleScrap(beanList);
		return result;
	}

	// 订单取消
	@RequestMapping(value = "/orderCancel")
	public @ResponseBody ResultBean orderCancel(@RequestBody List<TwBasWorkOrder> twBasWorkOrders) {
		ResultBean result=twBasWorkOrderService.orderCancel(twBasWorkOrders);
		return result;
	}

	// 已完工订单取消
	@RequestMapping(value = "/orderCompletedCancel")
	public @ResponseBody ResultBean orderCompletedCancel(@RequestBody OrderCancelBean orderCancelBean) {
		ResultBean result = twBasWorkOrderService.orderCompletedCancel(orderCancelBean);
		return result;
	}

	// 已生成VIN的订单取消
	@RequestMapping(value = "/orderOnlineCancel")
	public @ResponseBody ResultBean orderOnlineCancel(@RequestBody List<WorkOrderItem> workOrderItem) {
		ResultBean result = twBasWorkOrderService.orderOnlineCancel(workOrderItem);
		return result;
	}
}
